package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.interceptor.JwtTokenAdminInterceptor;
import com.sky.mapper.DishMapper;

import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 套餐业务实现
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }





    @Override
    public void add(SetmealVO setmealVO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        Long id = JwtTokenAdminInterceptor.threadLocal.get();
        setmeal.setCreateUser(id);
        setmeal.setUpdateUser(id);
        setmealMapper.addmeal(setmeal);

        Long setmealId = setmeal.getId();
        List<SetmealDish> list= setmealVO.getSetmealDishes();
        if(list!=null)
        {
            list.forEach(s->s.setDishId(setmeal.getId()));
          list.forEach(s->s.setSetmealId(setmealId));
            setmealDishMapper.add(list);
        }
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        Integer start =  (dishPageQueryDTO.getPage()-1)*dishPageQueryDTO.getPageSize();
        Integer pagesize = dishPageQueryDTO.getPageSize();
        dishPageQueryDTO.setStart(start);
        List<SetmealVO> setmealVOS = setmealMapper.page(dishPageQueryDTO);
        Integer count = setmealMapper.count();
        return new PageResult(count,setmealVOS);
    }

    @Override
    public void delete(List<Long> ids) {
        if (ids!=null&&ids.size()>0)
        {


        for (Long i:ids)
        {
            Setmeal setmeal = setmealMapper.selectById(i);
            if(setmeal.getStatus()==1)
            {
                throw  new RuntimeException(setmeal.getName()+"正在售卖，不能删除");
            }
        }

        setmealMapper.delete(ids);
        setmealDishMapper.delete(ids);
    }
    else {
    throw new BaseException("请选择要删除的商品");
    }
    }

    @Override
    public void setstatus(Integer status, Long id) {
        Setmeal setmeal  =new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    @Override
    public SetmealVO select(Long id) {
        Setmeal setmeal  = setmealMapper.selectById(id);
        List<SetmealDish> list = setmealDishMapper.select(id);
        SetmealVO setmealVO  =new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    @Override
    public void update(SetmealVO setmealVO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        setmealMapper.update(setmeal);

        List<SetmealDish> list = setmealVO.getSetmealDishes();
        List<Long> ids = new ArrayList<>();
        ids.add(setmeal.getId());
        //先删除对应的菜品
        setmealDishMapper.delete(ids);
        //再重新加
       list.forEach(s->s.setSetmealId(setmeal.getId()));
        setmealDishMapper.add(list);
    }


}

